package com.yckj.Dao;

import com.yckj.DbUtilHikari;
import com.yckj.Domain.User;
import jdk.internal.org.objectweb.asm.tree.IntInsnNode;

import java.net.ConnectException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.crypto.SecureUtil;
/**
 * @author wanghaonan
 */
public class UserDao {
    /**
     * 查询所有的用户
     * @return 包含所有的用户对象的集合
     */
    public List<User> selectAllUse(){
        //创建一个空集合，准备放用户对象
        List<User> userList = new ArrayList<User>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        conn = DbUtilHikari.getConnection();
        String sql = "SELECT userid,username,userpsw FROM users";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            //把认识里的数据转移到userList集合里
            while (rs.next()){
                //user当前是空的
                User user = new User();
                user.setUserid(rs.getInt("userid"));
                user.setUsername(rs.getString("username"));
                user.setUserpsw(rs.getString("userpsw"));
                userList.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtilHikari.close(rs,pstmt,conn);
        }

        return userList;
    }





    /**
     * 修改用户信息
     * @param userid 用户编号
     * @param uname 用户名
     * @param upsw 密码
     * @return 修改成功返回1，否则返回0
     */
    public int updateUser(int userid,String uname,String upsw){
        int flag = 0;
        Connection conn = null;
        PreparedStatement pstmt = null;

        conn = DbUtilHikari.getConnection();
        String sql = "UPDATE users SET username=?,userpsw=? WHERE userid=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));
            pstmt.setInt(3,userid);
            flag = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtilHikari.close(pstmt,conn);
        }
        return flag;
    }


    public int deleteUser(int userid){
        int flag = 0;
        //声明一些变量
        Connection conn = null;
        PreparedStatement pstmt = null;
        //获取数据库连接
        conn = DbUtilHikari.getConnection();
        //定义并准备SQL语句
        String sql = "DELETE FROM users WHERE userid=?";
        try {
            pstmt = conn.prepareStatement(sql);
            //给？赋值
            pstmt.setInt(1,userid);
            //执行SQL语句
            flag = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭资源（释放资源）
            DbUtilHikari.close(pstmt,conn);
        }
        return flag;
    }



        public boolean login(String uname,String upsw){
        boolean flag = false;
        Connection conn =null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        conn = DbUtilHikari.getConnection();
        String sql = "SELECT * FROM users WHERE username=? AND userpwd=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));
            rs = pstmt.executeQuery();
            if (rs.next()){
                flag = true;
            }else {
                flag = false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(rs,pstmt,conn);
        }


        return flag;

    }
    /**
     * 用户注册
     * @param uname 用户名
     * @param upsw 密码
     * @return 注册成功返回1.否则返回0
     */
    public int insertUser(String uname,String upsw){
        //声明变量
        //表示数据库的连接
        Connection conn = null;
        //预编译对象
        PreparedStatement pstmt = null;
        //注册成功的标志成功为1失败为0
        int flag =0;
        //获取数据连接
        conn = DbUtilHikari.getConnection();
        String sql = "INSERT INTO  users(username,userpsw) VALUES(?,?)";
        try {
            pstmt = conn.prepareStatement(sql);
            //问号赋值
            pstmt.setString(1,uname);
            pstmt.setString(2, SecureUtil.md5(upsw));
            //下达指令
            flag = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭数据库资源
            DbUtilHikari.close(pstmt,conn);
        }
        return flag;


    }
}
